package com.dingding.common.core.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import com.dingding.common.core.beans.activity.ActivityType;

/**
 * 
 * @Description: Java 对象序列化工具类
 * @author CHEHUANBO
 * @date 2016年12月8日
 * 
 * 
 */
public class SerializableUtil {

	/**
	 * 
	 * @Description: 序列化对象
	 * @param obj
	 *            待序列化对象
	 * @return byte[] 序列后的字节值
	 * @throws IOException
	 * @author CHEHUANBO
	 * 
	 */
	public static byte[] serialize(Object obj) throws IOException {

		if (obj == null) {
			throw new NullPointerException("序列化对象出现异常，原因 Object is null");
		}

		ByteArrayOutputStream bos = null;
		ObjectOutputStream objectOutputStream = null;
		try {

			bos = new ByteArrayOutputStream();
			objectOutputStream = new ObjectOutputStream(bos);
			objectOutputStream.writeObject(obj);

		} catch (IOException e) {
			throw e;
		} finally {
			if (objectOutputStream != null) {
				objectOutputStream.close();
			}
			if (bos != null) {
				bos.close();
			}
		}
		return bos.toByteArray();
	}

	/**
	 * 
	 * @Description: 序列化List集合
	 * @param objectList
	 *            待转换集合对象
	 * @return byte[] 序列后的字节值
	 * @throws IOException
	 * @author CHEHUANBO
	 * 
	 */
	public static <T> byte[] serialize(List<T> objectList) throws IOException {

		if (objectList == null) {
			throw new NullPointerException("序列化List集合出现异常，原因 List is null");
		}

		ByteArrayOutputStream bos = null;
		ObjectOutputStream objectOutputStream = null;
		try {

			if (objectList.size() > 0) {
				bos = new ByteArrayOutputStream();
				objectOutputStream = new ObjectOutputStream(bos);
				objectOutputStream.writeObject(objectList);
			}

		} catch (IOException e) {
			throw e;
		} finally {
			if (objectOutputStream != null) {
				objectOutputStream.close();
			}
			if (bos != null) {
				bos.close();
			}
		}
		return bos.toByteArray();
	}
	
	
	/**
	 * 
	 * @Description:反序列化字节为Java对象
	 * @param in
	 *            字节数组
	 * @return T 序列化好的Java对象
	 * @throws IOException
	 * @author CHEHUANBO
	 * @throws ClassNotFoundException
	 *             IOException
	 * 
	 */
	@SuppressWarnings("unchecked")
	public static <T> T deSerialize(byte[] in,Class<T> clazz)
			throws IOException, ClassNotFoundException {
        
		
		if (in == null || clazz == null) {
			throw new RuntimeException("反序列化对象出现异常，原因  clazz is null or byte[] is null");
		}

		
		T t = null;
		
		ByteArrayInputStream bis = null;
		ObjectInputStream ois = null;

		try {

			bis = new ByteArrayInputStream(in);
			ois = new ObjectInputStream(bis);

			t = (T) ois.readObject();

		} catch (IOException e) {
			throw e;
		} finally {

			if (ois != null) {
				ois.close();
			}

			if (bis != null) {
				bis.close();
			}

		}

		return t;

	}

	/**
	 * 
	 * @Description:反序列化字节为List集合
	 * @param in
	 *            字节数组
	 * @return List<T> 序列化好的集合对象
	 * @throws IOException
	 * @author CHEHUANBO
	 * @throws ClassNotFoundException
	 *             IOException
	 * 
	 */
	@SuppressWarnings("unchecked")
	public static <T> void deSerialize(byte[] in, List<T> deSerializeLists)
			throws IOException, ClassNotFoundException {

		if (in == null) {
			throw new RuntimeException("反序列化List集合出现异常，原因 List is null or byte[] is null");
		}

		ByteArrayInputStream bis = null;
		ObjectInputStream ois = null;

		try {

			bis = new ByteArrayInputStream(in);
			ois = new ObjectInputStream(bis);

			deSerializeLists = (List<T>) ois.readObject();

		} catch (IOException e) {
			throw e;
		} finally {

			if (ois != null) {
				ois.close();
			}

			if (bis != null) {
				bis.close();
			}

		}

	}

	/**
	 * 
	 * @Description: 序列化测试方法
	 * @throws IOException
	 * @author CHEHUANBO
	 * 
	 */
	public static void main(String[] args) throws IOException, ClassNotFoundException {

		List<String> strings = new ArrayList<String>();
		strings.add("张三");
		strings.add("李四");
		byte[] serBytes = SerializableUtil.serialize(strings);
		
		String str = "张三";
		byte[] strBytes = SerializableUtil.serialize(str);
		
		System.out.println(SerializableUtil.deSerialize(strBytes,String.class));
		

	}

}
